home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / jdk114 / demo / Clock / 102 / Clock2.jav
Encoding:
Text File  |  1997-09-11  |  5.6 KB  |  194 lines

  1. /*
  2.  * Copyright (c) 1994-1996 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
  6.  * without fee is hereby granted.
  7.  * Please refer to the file http://java.sun.com/copy_trademarks.html
  8.  * for further important copyright and trademark information and to
  9.  * http://java.sun.com/licensing.html for further important licensing
  10.  * information for the Java (tm) Technology.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  20.  * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  21.  * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  22.  * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  23.  * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  24.  * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  25.  * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
  26.  * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  27.  * HIGH RISK ACTIVITIES.
  28.  */
  29.  
  30. // author: Rachel Gollub, 1995
  31. // modified 96/04/24 Jim Hagen : use getBackground()
  32. // modified 96/05/29 Rachel Gollub : add garbage collecting
  33. // modified 96/10/22 Rachel Gollub : add bgcolor, fgcolor1, fgcolor2 params
  34. // Time!
  35.  
  36. import java.util.*;
  37. import java.awt.*;
  38. import java.applet.*;
  39.  
  40. public class Clock2 extends Applet implements Runnable {
  41.   Thread timer = null;
  42.   int lastxs=0, lastys=0, lastxm=0, lastym=0, lastxh=0, lastyh=0;
  43.   Date dummy = new Date();
  44.   String lastdate = dummy.toLocaleString();
  45.   Font F = new Font("TimesRoman", Font.PLAIN, 14);
  46.   Date dat = null;
  47.   Color fgcol = Color.blue;
  48.   Color fgcol2 = Color.darkGray;
  49.  
  50. public void init() {
  51.   int x,y;
  52.  
  53.   try {
  54.     setBackground(new Color(Integer.parseInt(getParameter("bgcolor"),16)));
  55.   } catch (Exception E) { }
  56.   try {
  57.     fgcol = new Color(Integer.parseInt(getParameter("fgcolor1"),16));
  58.   } catch (Exception E) { }
  59.   try {
  60.     fgcol2 = new Color(Integer.parseInt(getParameter("fgcolor2"),16));
  61.   } catch (Exception E) { }
  62.   resize(300,300);              // Set clock window size
  63. }
  64.  
  65.   // Plotpoints allows calculation to only cover 45 degrees of the circle,
  66.   // and then mirror
  67.  
  68. public void plotpoints(int x0, int y0, int x, int y, Graphics g) {
  69.  
  70.   g.drawLine(x0+x,y0+y,x0+x,y0+y);
  71.   g.drawLine(x0+y,y0+x,x0+y,y0+x);
  72.   g.drawLine(x0+y,y0-x,x0+y,y0-x);
  73.   g.drawLine(x0+x,y0-y,x0+x,y0-y);
  74.   g.drawLine(x0-x,y0-y,x0-x,y0-y);
  75.   g.drawLine(x0-y,y0-x,x0-y,y0-x);
  76.   g.drawLine(x0-y,y0+x,x0-y,y0+x);
  77.   g.drawLine(x0-x,y0+y,x0-x,y0+y);
  78. }
  79.  
  80.   // Circle is just Bresenham's algorithm for a scan converted circle
  81.  
  82. public void circle(int x0, int y0, int r, Graphics g) {
  83.   int x,y;
  84.   float d;
  85.  
  86.   x=0;
  87.   y=r;
  88.   d=5/4-r;
  89.   plotpoints(x0,y0,x,y,g);
  90.  
  91.   while (y>x){
  92.     if (d<0) {
  93.       d=d+2*x+3;
  94.       x++;
  95.     }
  96.     else {
  97.       d=d+2*(x-y)+5;
  98.       x++;
  99.       y--;
  100.     }
  101.     plotpoints(x0,y0,x,y,g);
  102.   }
  103. }
  104.  
  105.  
  106.   // Paint is the main part of the program
  107.  
  108. public void paint(Graphics g) {
  109.   int xh, yh, xm, ym, xs, ys, s, m, h, xcenter, ycenter;
  110.   String today;
  111.  
  112.   dat = new Date();
  113.   s = dat.getSeconds();
  114.   m = dat.getMinutes();
  115.   h = dat.getHours();
  116.   today = dat.toLocaleString();
  117.   xcenter=80;
  118.   ycenter=55;
  119.   
  120.   // a= s* pi/2 - pi/2 (to switch 0,0 from 3:00 to 12:00)
  121.   // x = r(cos a) + xcenter, y = r(sin a) + ycenter
  122.   
  123.   xs = (int)(Math.cos(s * 3.14f/30 - 3.14f/2) * 45 + xcenter);
  124.   ys = (int)(Math.sin(s * 3.14f/30 - 3.14f/2) * 45 + ycenter);
  125.   xm = (int)(Math.cos(m * 3.14f/30 - 3.14f/2) * 40 + xcenter);
  126.   ym = (int)(Math.sin(m * 3.14f/30 - 3.14f/2) * 40 + ycenter);
  127.   xh = (int)(Math.cos((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 + xcenter);
  128.   yh = (int)(Math.sin((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 + ycenter);
  129.   
  130.   // Draw the circle and numbers
  131.   
  132.   g.setFont(F);
  133.   g.setColor(fgcol);
  134.   circle(xcenter,ycenter,50,g);
  135.   g.setColor(fgcol2);
  136.   g.drawString("9",xcenter-45,ycenter+3); 
  137.   g.drawString("3",xcenter+40,ycenter+3);
  138.   g.drawString("12",xcenter-5,ycenter-37);
  139.   g.drawString("6",xcenter-3,ycenter+45);
  140.  
  141.   // Erase if necessary, and redraw
  142.   
  143.   g.setColor(getBackground());
  144.   if (xs != lastxs || ys != lastys) {
  145.     g.drawLine(xcenter, ycenter, lastxs, lastys);
  146.     g.drawString(lastdate, 5, 125);
  147.   }
  148.   if (xm != lastxm || ym != lastym) {
  149.     g.drawLine(xcenter, ycenter-1, lastxm, lastym);
  150.     g.drawLine(xcenter-1, ycenter, lastxm, lastym); }
  151.   if (xh != lastxh || yh != lastyh) {
  152.     g.drawLine(xcenter, ycenter-1, lastxh, lastyh);
  153.     g.drawLine(xcenter-1, ycenter, lastxh, lastyh); }
  154.   g.setColor(fgcol2);
  155.   g.drawString(today, 5, 125);  
  156.   g.drawLine(xcenter, ycenter, xs, ys);
  157.   g.setColor(fgcol);
  158.   g.drawLine(xcenter, ycenter-1, xm, ym);
  159.   g.drawLine(xcenter-1, ycenter, xm, ym);
  160.   g.drawLine(xcenter, ycenter-1, xh, yh);
  161.   g.drawLine(xcenter-1, ycenter, xh, yh);
  162.   lastxs=xs; lastys=ys;
  163.   lastxm=xm; lastym=ym;
  164.   lastxh=xh; lastyh=yh;
  165.   lastdate = today;
  166.   dat=null;
  167. }
  168.  
  169. public void start() {
  170.   if(timer == null)
  171.     {
  172.       timer = new Thread(this);
  173.       timer.start();
  174.     }
  175. }
  176.  
  177. public void stop() {
  178.   timer = null;
  179. }
  180.  
  181. public void run() {
  182.   while (timer != null) {
  183.     try {Thread.sleep(100);} catch (InterruptedException e){}
  184.     repaint();
  185.   }
  186.   timer = null;
  187. }
  188.  
  189. public void update(Graphics g) {
  190.   paint(g);
  191. }
  192. }
  193.  
  194.